import Vue from 'vue'
import Router from 'vue-router'
import RouterCommon from './common' // 引入通用模块
import Active from './active'
import Info from './info'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {Dialog} from 'vant'
import wx from 'weixin-js-sdk'

NProgress.configure({
    easing: 'ease', // 动画方式
    speed: 500, // 递增进度条的速度
    showSpinner: false, // 是否显示加载ico
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3 // 初始化时的最小百分比
})

Vue.use(Router)
Vue.use(Dialog);

const router = new Router({
    routes: [
        ...RouterCommon,
        ...Active,
        ...Info,
        // {
        //     path: '/orderTicket',
        //     name: 'orderTicket',
        //     component: resolve => require(['@/pages/orderTicket/index'], resolve),
        //     meta: {
        //         title: '景区门票',
        //         keepAlive: false
        //     }
        // },
        {
            path: '/personTicket',
            name: 'personTicket',
            component: resolve => require(['@/pages/orderTicket/person/index'], resolve),
            meta: {
                title: '散客预约',
                keepAlive: false
            }
        },
        {
            path: '/teamTicket',
            name: 'teamTicket',
            component: resolve => require(['@/pages/orderTicket/team/index'], resolve),
            meta: {
                title: '团队预约',
                keepAlive: false
            }
        },
        {
            path: '/personOrder',
            name: 'personOrder',
            component: resolve => require(['@/pages/orderTicket/person/personOrder'], resolve),
            meta: {
                title: '散客预约',
                keepAlive: false
            }
        },
        {
            path: '/teamOrder',
            name: 'teamOrder',
            component: resolve => require(['@/pages/orderTicket/team/teamOrder'], resolve),
            meta: {
                title: '团体预约',
                keepAlive: false
            }
        },
        {
            path: '/success',
            name: 'success',
            component: resolve => require(['@/pages/orderTicket/success'], resolve),
            meta: {
                // title: '散客预约',
                keepAlive: false
            }
        },
        {
            path: '/publicTicket',
            name: 'publicTicket',
            component: resolve => require(['@/pages/orderTicket/publicTicket'], resolve),
            meta: {
                // title: '散客预约',
                keepAlive: false
            }
        },
        {
            path: '/ticket',
            name: 'ticket',
            component: resolve => require(['@/pages/orderTicket/index2'], resolve),
            meta: {
                // title: '散客预约',
                keepAlive: false
            }
        },
        {
            path: '/personDetail',
            name: 'detail',
            component: resolve => require(['@/pages/orderTicket/person/personDetail'], resolve),
            meta: {
                title: '门票详情',
                keepAlive: false
            }
        },
        {
            path: '/teamDetail',
            name: 'detail',
            component: resolve => require(['@/pages/orderTicket/team/teamDetail'], resolve),
            meta: {
                title: '门票详情',
                keepAlive: false
            }
        }
    ],
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        } else {
            return {x: 0, y: 0};
        }
    },
})

const cancelLastReq = (next) => {
    let axiosPromiseArr = window.__axiosPromiseArr || []
    if (!!axiosPromiseArr && axiosPromiseArr.length) {
        // console.log(axiosPromiseArr)
        let len = axiosPromiseArr.length
        while (len--) {
            axiosPromiseArr[len].cancel('cancel')
            axiosPromiseArr.splice(len, 1)
        }
    }
    next()
}

/**
 * 获取token
 */
const getCookie = cname => {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

/**
 * 微信环境判断
 */
const isWxEnv = () => {
    let ua = navigator.userAgent.toLowerCase();
    return ua.match(/MicroMessenger/i) == "micromessenger";
};
router.beforeEach((to, from, next) => {
    if (to.meta.needLogin && process.env.NODE_ENV == 'production') {// && process.env.NODE_ENV == 'production'
        if (!isWxEnv()) {
            Dialog.alert({
                title: '请在微信环境下使用',
                message: '',
            }).then(() => {

            });
        } else {
            // location.href = "/wx_oauth?vue_url=" + window.location.hash.replace('#','')
            // 如果没有api_token 就返回首页
            // 清除token
            localStorage.removeItem('api_token');
            Dialog.alert({
                title: '登录已失效',
                message: '',
            }).then(() => {
                wx.miniProgram.switchTab({
                    url: '/pages/index/index/index',
                });
            });
        }
    } else {
        NProgress.start()
        cancelLastReq(next)
    }
})

router.afterEach((to, from) => {
    // 在即将进入新的页面组件前，关闭掉进度条
    NProgress.done()
})

export default router
